﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chapter3Applications.Models.ViewModels;
using Chapter3Applications.Models.Abstract;

using Chapter3Applications.Models.PersistentModel;

namespace Chapter3Applications.Controllers
{
    public class HomeController : Controller
    {
        private IShoppingListRepository model;

        public HomeController(IShoppingListRepository modelParam)
        {
            model = modelParam;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult EnterProduct()
        {
            return View();
        }

        [HttpPost]
        public ActionResult EnterProduct(Product product)
        {
            //Remark: so that the fields are empty of any submit element
            // the PRG (post - redirect - get) pattern must be followed!
            if (ModelState.IsValid)
            {
                model.Add(product);
                model.SaveChanges();
                //After redirect only TempData is still present
                TempData["productName"] = product.ProductName;
                return RedirectToAction("EnterProduct");
            }
            else
            {
                return View();
            }
        }

        public ActionResult DisplayProducts()
        {
            List<String> ProductNameList = new List<String>();
            model.ProductSet.ToList<Product>().ForEach(x => ProductNameList.Add(x.ProductName));

            return View(ProductNameList);
        }

        [HttpGet]
        public ActionResult EnterShoppingList()
        {
            EnterShoppingListViewModel viewmodel = new EnterShoppingListViewModel();
            model.ProductSet.ToList().ForEach(x => viewmodel.Products.Add(x.ProductName, new Boolean()));

            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult EnterShoppingList(EnterShoppingListViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                ShoppingList shoppingList = new ShoppingList();
                shoppingList.ShoppingListName = viewmodel.ShoppingListName;

                foreach(KeyValuePair<String, Boolean> myProduct in viewmodel.Products)
                {
                    if (myProduct.Value == true)
                    {
                        shoppingList.Product.Add( model.ProductSet.ToList().Where(x => x.ProductName == myProduct.Key).First() );
                    }
                }
                
                model.Add(shoppingList);

                model.SaveChanges();

                TempData["shoppingListName"] = shoppingList.ShoppingListName;
                return RedirectToAction("EnterShoppingList");
            }
            else
            { 
                return View(viewmodel);
            }      
        }

        public ActionResult DisplayShoppingLists()
        {
            return View(model);
        }

        [HttpGet]
        public ActionResult DeleteProducts()
        {
            DeleteItemsViewModel viewmodel = new DeleteItemsViewModel();
            model.ProductSet.ToList().ForEach(x => viewmodel.Items.Add(x.ProductName, new Boolean()));

            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult DeleteProducts(DeleteItemsViewModel viewmodel)
        {
            foreach(KeyValuePair<String, Boolean> Product in viewmodel.Items.Where(x => x.Value == true))
            {
                foreach (Product myProduct in model.ProductSet.Where(x => x.ProductName == Product.Key))
                {
                    model.Remove(myProduct);
                }

                foreach (ShoppingList myShoppingList in model.ShoppingListSet)
                {
                    if (myShoppingList.Product.Where(x => x.ProductName == Product.Key).Any())
                    {
                        myShoppingList.Product.Remove(myShoppingList.Product.Where(x => x.ProductName == Product.Key).First());
                    }
                }
            }

            model.SaveChanges();

            return RedirectToAction("DeleteProducts");
        }

        [HttpGet]
        public ActionResult DeleteShoppingLists()
        {
            DeleteItemsViewModel viewmodel = new DeleteItemsViewModel();
            model.ShoppingListSet.ToList().ForEach(x => viewmodel.Items.Add(x.ShoppingListName, new Boolean()));

            return View(viewmodel);
        }

        [HttpPost]
        public ActionResult DeleteShoppingLists(DeleteItemsViewModel viewmodel)
        {  
            foreach (KeyValuePair<String, Boolean> ShoppingListName in viewmodel.Items.Where(x => x.Value == true))
            {
                model.Remove(model.ShoppingListSet.Where(x => x.ShoppingListName == ShoppingListName.Key).First());

                foreach (Product myProduct in model.ProductSet)
                {
                    if (myProduct.ShoppingList.Where(x => x.ShoppingListName == ShoppingListName.Key).Any())
                    {
                        myProduct.ShoppingList.Remove(myProduct.ShoppingList.Where(x => x.ShoppingListName == ShoppingListName.Key).First());
                    }
                }
            }

            model.SaveChanges();

            return RedirectToAction("DeleteShoppingLists");
        }
    }
}
